package com.thtn.greendroidthtneg2;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

public class DictionaryProvider extends ContentProvider {
	public static String AUTHORITY = "com.thtn.greendroidthtneg2.DictionaryProvider";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/database");
	public static final String DEFINITION_MIME_TYPE = "vnd.android.cursor.item/vnd.kreatorz";
	private static final int GET_WORD = 1;
	private static final int SEARCH_SUGGEST = 2;
	private static final int SEARCH_WORDS = 0;
	public static final String WORDS_MIME_TYPE = "vnd.android.cursor.dir/vnd.kreatorz";
	private static final UriMatcher sURIMatcher = buildUriMatcher();
	String TAG = "DictionaryProvider";
	DBAdapter db = null;

	private Cursor bookmarkList(String paramString) {
		return this.db.bookmarkList(paramString);
	}

	private static UriMatcher buildUriMatcher() {
		UriMatcher localUriMatcher = new UriMatcher(-1);
		localUriMatcher.addURI(AUTHORITY, "database", 0);
		localUriMatcher.addURI(AUTHORITY, "database/#", 1);
		localUriMatcher.addURI(AUTHORITY, "search_suggest_query", 2);
		localUriMatcher.addURI(AUTHORITY, "search_suggest_query/*", 2);
		return localUriMatcher;
	}

	private Cursor getRandom() {
		return this.db.getRandom();
	}

	private Cursor getSuggestions(String paramString) {
		return this.db.getSuggestions(paramString);
	}

	private Cursor getWord(Uri paramUri) {
		return this.db.getWord(paramUri);
	}

	private Cursor wordList(String paramString) {
		return this.db.wordList(paramString);
	}

	public int delete(Uri paramUri, String paramString,
			String[] paramArrayOfString) {
		this.db.delete(paramUri, paramString, paramArrayOfString);
		return 1;
	}

	public String getType(Uri paramUri) {
		switch (sURIMatcher.match(paramUri)) {
		default:
			throw new IllegalArgumentException("Unknown URL " + paramUri);
		case 0:
			return "vnd.android.cursor.dir/vnd.kreatorz";
		case 1:
			return "vnd.android.cursor.item/vnd.kreatorz";
		case 2:
			return "vnd.android.cursor.dir/vnd.android.search.suggest";
		}

	}

	public Uri insert(Uri paramUri, ContentValues paramContentValues) {
		this.db.insert(paramUri, paramContentValues);
		return paramUri;
	}

	public boolean onCreate() {
		this.db = new DBAdapter(getContext().getApplicationContext());
		return true;
	}

	public Cursor query(Uri paramUri, String[] paramArrayOfString1,
			String paramString1, String[] paramArrayOfString2,
			String paramString2) {
		switch (sURIMatcher.match(paramUri)) {
		default:
			throw new IllegalArgumentException("Unknown Uri: " + paramUri);
		case 2:
			if (paramArrayOfString2 == null)
				throw new IllegalArgumentException(
						"selectionArgs must be provided for the Uri: "
								+ paramUri);
			return getSuggestions(paramArrayOfString2[0]);
		case 0:
			if (paramArrayOfString2 == null)
				return bookmarkList(paramString1);
			return wordList(paramArrayOfString2[0]);
		case 1:
			if (paramString2 != null)
				return getRandom();
			return getWord(paramUri);
		}

	}

	public int update(Uri paramUri, ContentValues paramContentValues,
			String paramString, String[] paramArrayOfString) {
		this.db.updateRowData("_id='" + paramUri.getLastPathSegment() + "'",
				paramContentValues);
		return 1;
	}

}
